<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
<!-- Copyright 1997 The Open Group, All Rights Reserved -->
<title>basename</title>
</head><body bgcolor=white>
<center>
<font size=2>
The Single UNIX &reg; Specification, Version 2<br>
Copyright &copy; 1997 The Open Group

</font></center><hr size=2 noshade>
<h4><a name = "tag_000_000_115">&nbsp;</a>NAME</h4><blockquote>
basename - return non-directory portion of a pathname
</blockquote><h4><a name = "tag_000_000_116">&nbsp;</a>SYNOPSIS</h4><blockquote>
<pre><code>

basename <i>string </i><b>[</b><i>suffix</i><b>]</b>
</code>
</pre>
</blockquote><h4><a name = "tag_000_000_117">&nbsp;</a>DESCRIPTION</h4><blockquote>
The
<i>string</i>
operand will be treated as a pathname, as defined in
<b>Pathname</b>.
The string
<i>string</i>
will be converted to the filename corresponding to
the last pathname component in
<i>string</i>
and then the suffix string
<i>suffix</i>,
if present, will be removed.
This will be done by performing actions
equivalent to the following steps in order:
<ol>
<p>
<li>
If
<i>string</i>
is
<b>//</b>,
it is implementation-dependent whether steps 2 to 5 are
skipped or processed.
<p>
<li>
If
<i>string</i>
consists entirely of slash characters,
<i>string</i>
will be set to a single slash character.
In this case, skip steps 3 to 5.
<p>
<li>
If there are any trailing slash characters in
<i>string</i>,
they will be removed.
<p>
<li>
If there are any slash characters remaining in
<i>string</i>,
the prefix of
<i>string</i>
up to and including the last slash character in
<i>string</i>
will be removed.
<p>
<li>
If the
<i>suffix</i>
operand is present, is not identical
to the characters remaining in
<i>string</i>,
and is identical to a suffix of the characters remaining in
<i>string</i>,
the suffix
<i>suffix</i>
will be removed from
<i>string</i>.
Otherwise,
<i>string</i>
will not be modified by this step.
It will not be considered an error if
<i>suffix</i>
is not found in
<i>string</i>.
<p>
</ol>
<p>
The resulting string will be written to standard output.
</blockquote><h4><a name = "tag_000_000_118">&nbsp;</a>OPTIONS</h4><blockquote>
None.
</blockquote><h4><a name = "tag_000_000_119">&nbsp;</a>OPERANDS</h4><blockquote>
The following operands are supported:
<dl compact>

<dt><i>string</i><dd>A string.

<dt><i>suffix</i><dd>A string.

</dl>
</blockquote><h4><a name = "tag_000_000_120">&nbsp;</a>STDIN</h4><blockquote>
Not used.
</blockquote><h4><a name = "tag_000_000_121">&nbsp;</a>INPUT FILES</h4><blockquote>
None.
</blockquote><h4><a name = "tag_000_000_122">&nbsp;</a>ENVIRONMENT VARIABLES</h4><blockquote>
The following environment variables affect the execution of
<i>basename</i>:
<dl compact>

<dt><i>LANG</i><dd>Provide a default value for the internationalisation variables
that are unset or null.
If
<i>LANG</i>
is unset or null, the corresponding value from the
implementation-dependent default locale will be used.
If any of the internationalisation variables contains an invalid setting, the
utility will behave as if none of the variables had been defined.

<dt><i>LC_ALL</i><dd>
If set to a non-empty string value,
override the values of all the other internationalisation variables.

<dt><i>LC_CTYPE</i><dd>
Determine the
locale for the interpretation of sequences of bytes of text data as
characters (for example, single- as opposed to multi-byte characters
in arguments).

<dt><i>LC_MESSAGES</i><dd>
Determine the locale that should be used to affect
the format and contents of diagnostic
messages written to standard error.

<dt><i>NLSPATH</i><dd>
Determine the location of message catalogues
for the processing of
<i>LC_MESSAGES .
</i>
</dl>
</blockquote><h4><a name = "tag_000_000_123">&nbsp;</a>ASYNCHRONOUS EVENTS</h4><blockquote>
Default.
</blockquote><h4><a name = "tag_000_000_124">&nbsp;</a>STDOUT</h4><blockquote>
The
<i>basename</i>
utility will write a line to the standard output
in the following format:
<code>
<p>
<tt>"%s\n"</tt>, &lt;<i>resulting string</i>&gt;
</code>
</blockquote><h4><a name = "tag_000_000_125">&nbsp;</a>STDERR</h4><blockquote>
Used only for diagnostic messages.
</blockquote><h4><a name = "tag_000_000_126">&nbsp;</a>OUTPUT FILES</h4><blockquote>
None.
</blockquote><h4><a name = "tag_000_000_127">&nbsp;</a>EXTENDED DESCRIPTION</h4><blockquote>
None.
</blockquote><h4><a name = "tag_000_000_128">&nbsp;</a>EXIT STATUS</h4><blockquote>
The following exit values are returned:
<dl compact>

<dt>0<dd>Successful completion.

<dt>&gt;0<dd>An error occurred.

</dl>
</blockquote><h4><a name = "tag_000_000_129">&nbsp;</a>CONSEQUENCES OF ERRORS</h4><blockquote>
Default.
</blockquote><h4><a name = "tag_000_000_130">&nbsp;</a>APPLICATION USAGE</h4><blockquote>
The definition of
<i>pathname</i>
specifies implementation-dependent behaviour for pathnames
starting with two slash characters.
Therefore, applications must not arbitrarily add slashes
to the beginning of a pathname unless they can ensure
that there are more or less than two or are prepared
to deal with the implementation-dependent consequences.
</blockquote><h4><a name = "tag_000_000_131">&nbsp;</a>EXAMPLES</h4><blockquote>
If the string
<i>string</i>
is a valid pathname:
<pre>
<code>
$(basename "string")
</code>
</pre>
produces a filename that could be used to open the file named by
<i>string</i>
in the directory returned by:
<pre>
<code>
$(dirname "string")
</code>
</pre>
<p>
If the string
<i>string</i>
is not a valid pathname, the same algorithm
is used, but the result need not be a valid filename.
The
<i>basename</i>
utility is not expected to make any judgements about
the validity of
<i>string</i>
as a pathname; it just follows the
specified algorithm to produce a result string.
<p>
The following shell script compiles
<b>/usr/src/cmd/cat.c</b>
and moves the output to a file named
<b>cat</b>
in the current directory when
invoked with the argument
/usr/src/cmd/cat
or with the argument
/usr/src/cmd/cat.c:
<pre>
<code>
c89 $(dirname "$1")/$(basename "$1" .c).c
mv a.out $(basename "$1" .c)
</code>
</pre>
</blockquote><h4><a name = "tag_000_000_132">&nbsp;</a>FUTURE DIRECTIONS</h4><blockquote>
None.
</blockquote><h4><a name = "tag_000_000_133">&nbsp;</a>SEE ALSO</h4><blockquote>
<i><a href="dirname.html">dirname</a></i>,
<xref href=parms><a href="chap2.html#tag_001_005">
Parameters and Variables
</a></xref>.
</blockquote><hr size=2 noshade>
<center><font size=2>
UNIX &reg; is a registered Trademark of The Open Group.<br>
Copyright &copy; 1997 The Open Group
<br> [ <a href="../index.html">Main Index</a> | <a href="../xshix.html">XSH</a> | <a href="../xcuix.html">XCU</a> | <a href="../xbdix.html">XBD</a> | <a href="../cursesix.html">XCURSES</a> | <a href="../xnsix.html">XNS</a> ]

</font></center><hr size=2 noshade>
</body></html>
